//
//  ZLEmotionToolBar.swift
//  表情键盘
//
//  Created by zhanglongtao on 17/1/13.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import UIKit

@objc protocol ZLEmotionToolBarDelegate: NSObjectProtocol {
    
    func emotionToolbarSelectedItem(toolbar: ZLEmotionToolBar, index: Int)
}

//表情键盘底部工具栏
class ZLEmotionToolBar: UIView {

    //代理
    weak var delegate: ZLEmotionToolBarDelegate?
    //
    var selectedindex: Int = 0{
        
        didSet{
            //取消所有按钮的选中状态
            for btn in subviews as! [UIButton]{
                
                btn.isSelected = false
                
            }
            
            (subviews[selectedindex] as? UIButton)?.isSelected = true
        }
    }
    
    override func awakeFromNib() {
        
        setupUI()
    }
    override func layoutSubviews() {
        //布局所有按钮
        let count = subviews.count
        let w = bounds.width / CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: bounds.height)
        for (i, btn) in subviews.enumerated(){
            btn.frame = rect.offsetBy(dx: CGFloat(i) * w, dy: 0)
        }
        
    }
    
    @objc fileprivate func clickItem(button: UIButton){
        
        //通知代理执行协议方法
        
        delegate?.emotionToolbarSelectedItem(toolbar: self, index: button.tag)
        
    }

}
private extension ZLEmotionToolBar {
    func setupUI() {
        
        let manger = EmotionManager.shared
        
        for (i, p) in manger.packages.enumerated() {
            
            let btn = UIButton()
            
            btn.setTitle(p.groupName, for: [])
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            
            btn.setTitleColor(UIColor.white, for: [])
            btn.setTitleColor(UIColor.darkGray, for: .highlighted)
            btn.setTitleColor(UIColor.darkGray, for: .selected)
            ///设置按钮的图片
            let imageName = "compose_emotion_table_\(p.bgImageName ?? "")_normal"
            let imageNameHL = "compose_emotion_table_\(p.bgImageName ?? "")_selected"
            var image = UIImage(named: imageName, in: manger.bundle, compatibleWith: nil)
            
            var imageHL = UIImage(named: imageNameHL, in: manger.bundle, compatibleWith: nil)
            
            let size = image?.size ?? CGSize()
            let inset = UIEdgeInsets(top: size.height * 0.5, left: size.width * 0.5, bottom: size.height * 0.5, right: size.width * 0.5)
            image = image?.resizableImage(withCapInsets: inset)
            imageHL = imageHL?.resizableImage(withCapInsets: inset)
            
            btn.setBackgroundImage(image, for: [])
            btn.setBackgroundImage(imageHL, for: .highlighted)
            btn.setBackgroundImage(imageHL, for: .selected)
            
            //设置button的tag
            btn.tag = i
            
            btn.sizeToFit()
            addSubview(btn)
            
            btn.addTarget(self, action: #selector(clickItem(button:)), for: .touchUpInside)
            
        }
        //默认选中第0个按钮
        (subviews[0] as? UIButton)?.isSelected = true
    }
}
